package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_contract_detail.entity.WxContractDetail;
import com.sdp.wx.db.wx_contract_detail.service.IWxContractDetailService;
import com.sdp.wx.db.wx_contract_detail.service.impl.WxContractDetailServiceImpl;
import com.sdp.wx.utils.WxConstants;
import com.sdp.wx.view.ContractView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/WxContractDetailController")
public class WxContractDetailController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxContractDetailService iWxContractDetailService;
	
	@Autowired
	private WxContractDetailServiceImpl wxContractDetailServiceImpl;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxContractDetailService, WxContractDetail.class, dataCenter));
	}
	
	@PostMapping(value = "/viewPage")
	public Result<?> pageView(@RequestBody DataCenter dataCenter) {
		QueryWrapper<ContractView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(wxContractDetailServiceImpl, ContractView.class, dataCenter,queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxContractDetail contractDetail) {
		if(contractDetail==null) {
			return Result.error("合同详情信息为空");
		}
		if(StringUtils.isEmpty(contractDetail.getKeyId())) {
			contractDetail.setRepairStatus(WxConstants.维修状态_已排计划.getCode());
			iWxContractDetailService.save(contractDetail);
		}else {
		
			iWxContractDetailService.updateById(contractDetail);
		}
		
		return Result.ok();
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("传入参数不完整，不能删除");
		}
		iWxContractDetailService.removeById(keyId);
		return Result.ok();
	}
}

